import entity.board;
import java.io.*;

public class txtIO {

	/*
	 * If the given file exists, read and parse it's content. Use the result to
	 * initiate a board which will be returned. If something goes wrong, error
	 * information will be printed and the method will return null.
	 */
	public static board readFile(String fileName) {
		board b = null;
		try {
			File file = new File(fileName);
			if (file.isFile() && file.exists()) {

				// read the content to String object text.
				InputStreamReader reader = new InputStreamReader(
						new FileInputStream(file));
				BufferedReader bfReader = new BufferedReader(reader);
				String text = "";
				String line;
				while ((line = bfReader.readLine()) != null) {
					text += line;
				}
				bfReader.close();
				// Parse the content.The array stores the data.
				int[] numbers = getNumbers(text);
				int index = 0;
				int width = numbers[index++];
				int UPCost = numbers[index++];
				int RIGHTCost = numbers[index++];
				int DOWNCost = numbers[index++];
				int LEFTCost = numbers[index++];
				int ax = numbers[index++];
				int ay = numbers[index++];
				int bx = numbers[index++];
				int by = numbers[index++];
				int obstacleNumbers = (numbers.length - 9) / 2;

				// create board
				b = new board(width, obstacleNumbers);
				if (!b.putAgentA(ax, ay, new int[] { UPCost, RIGHTCost,
						DOWNCost, LEFTCost })) {
					System.out.println("Position("+ax+","+ay+") of A in the file is incorrect!");
					return null;

				}
				if(!b.putAgentB(bx, by, new int[] { UPCost, RIGHTCost, DOWNCost,
						LEFTCost })){
					System.out.println("Position("+bx+","+by+") of B in the file is incorrect!");
					return null;
				}
				for (; index < numbers.length;) {
					int ox = numbers[index++];
					int oy = numbers[index++];
					if(!b.setObstacle(ox, oy)){
						System.out.println("Position("+ox+","+oy+") of obstacles in the file is incorrect!");
						return null;
					}
				}
				
				return b;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("File not found!");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("File has wrong format!");
		}
		return null;

	}

	/*
	 * Append the given content to the given file's tail. If the file doesn't
	 * exit,it will create a new one.
	 */
	public static void appendFile(String fileName, String content) {
		File file = new File(fileName);
		try {
			OutputStreamWriter writer = new OutputStreamWriter(
					new FileOutputStream(file, true));
			writer.append(content);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * Write the given content to the given file. If the file doesn't exit,it
	 * will create a new one. If it exists,the old content will be erased.
	 */
	public static void writeFile(String fileName, String content) {
		File file = new File(fileName);
		try {
			OutputStreamWriter writer = new OutputStreamWriter(
					new FileOutputStream(file));
			writer.append(content);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * Parse the string to numbers.
	 * It's need to be improved.
	 */
	private static int[] getNumbers(String s) {
		int[] numbers = new int[s.length()];
		int indexOfNumbers = 0;
		int indexOfString = 0;
		for (; indexOfString < s.length(); indexOfString++) {
			if (s.charAt(indexOfString) >= '1'
					&& s.charAt(indexOfString) <= '9') {
				String temp = s.substring(indexOfString, indexOfString + 1);
				numbers[indexOfNumbers++] = Integer.parseInt(temp);
			}
		}
		int[] result = new int[indexOfNumbers];
		for (int i = 0; i < result.length; i++) {
			result[i] = numbers[i];
		}
		return result;
	}
}
